package com.scs.application.modules.purchase.controller;

import cn.hutool.core.util.ObjectUtil;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.base.annotation.MatrCanPass;
import com.scs.application.modules.purchase.dto.OrderItemByPackDTO;
import com.scs.application.modules.purchase.entity.OrderItem;
import com.scs.application.modules.purchase.service.OrderItemService;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单明细 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/purchase/order_item")
public class OrderItemController extends CrudController<OrderItemService, OrderItem>{


    /**
     * 添加/更新数据
     */
    @PostMapping("/save")
    @MatrCanPass(businessType = "purchase_item")
    public RestResult saveItem(@Valid @RequestBody OrderItem entity) {
        boolean isOk = service.saveOrUpdate(entity);
        if (!isOk) {
            return RestResult.error("数据保存失败");
        }
        return RestResult.ok("数据保存成功", entity);
    }


    /**
     * 跟台包采购：添加跟台包明细
     *
     * @param orderItemByPackDTO
     * @return
     */
    @PostMapping("addByPack")
    public RestResult addByPack(@RequestBody OrderItemByPackDTO orderItemByPackDTO) {
        return RestResult.ok("添加成功", service.addByPack(orderItemByPackDTO));
    }

    /**
     * 修改包装数量及主单金额，包金额
     *
     * @param orderItem
     * @return
     */
    @PostMapping("/updateQty")
    public RestResult updateQty(@RequestBody OrderItem orderItem) {
        boolean isSuccess = service.updateQty(orderItem);
        if (!isSuccess) {
            return RestResult.error("删除失败");
        }
        return RestResult.ok("已成功更新数量及金额");
    }


    /**
     * 删除跟台包采购明细
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deletePackage")
    public RestResult deletePackage(@RequestParam("id") String id) {
        boolean isSuccess = service.deletePackage(id);
        if (!isSuccess) {
            return RestResult.error("删除失败");
        }
        return RestResult.ok("删除成功");
    }

    /**
     * 删除跟台包内耗材
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteMatr")
    public RestResult deleteMatr(@RequestParam("id") String id) {
        boolean isSuccess = service.deleteMatr(id);
        if (!isSuccess) {
            return RestResult.error("删除失败");
        }
        return RestResult.ok("删除成功");
    }

    /**
     * @Description：批量操作
     */
    @PostMapping("/saveBatchMatr")
    public RestResult saveBatchMatr(@RequestBody Map<String, Object> para) {
        List<Serializable> matrIdList = (List<Serializable>) para.get("matrIdList");
        BusinessException.throwErrorIf(ObjectUtil.length(matrIdList) == 0,"缺少参数【matrIdList】");
        BusinessException.throwErrorIf(StringUtils.isBlank(para.get("orderId")+""),"缺少参数【orderId】");
        service.saveBatchMatr(matrIdList,para.get("orderId")+"");
        return RestResult.ok("批量添加成功");
    }
}
